<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    body {
      height: 100vh;
      background-color: rgb(180, 8, 180);
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .panel {
      width: 400px;
      height: 150px;
      background-color: #eee;
      border-radius: 10px;
      transition: all 1s linear;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
    }
  </style>
</head>

<body>
  <div class="panel"></div>
</body>
<script>
  let panel = document.querySelector('.panel');
  document.body.addEventListener('mousedown', (e) => {
    let prevx = e.clientX;
    let prevy = e.clientY;
    let x = 0,
      y = 0;
    document.body.addEventListener('mousemove', (ev) => {
      // console.log(ev.clientX,ev.clientY);
      if (ev.clientX > prevx) {
        console.log('鼠标右移');
        prevx = ev.clientX;
        panel.style.transform = `rotateY(${x--}deg) rotateX(${y}deg)`;
        if (x <= 15) x = -15;
      } else {
        console.log('鼠标左移');
        prevx = ev.clientX;
        panel.style.transform = `rotateY(${x++}deg) rotateX(${y}deg)`;
        if (x >= 15) x = 15;
      }

      if (ev.clientY > prevy) {
        console.log('鼠标下移');
        prevy = ev.clientY;
        panel.style.transform = `rotateX(${y++}deg) rotateY(${x}deg)`;
        if (y >= 15) y = 15;
      } else {
        console.log('鼠标上移');
        prevy = ev.clientY;
        panel.style.transform = `rotateX(${y--}deg) rotateY(${x}deg)`;
        if (y <= -15) y = -15;
      }
    });
  });
</script>

</html>